package com.sunmnet.j2ee.core.powerdb.designer;

import com.sunmnet.j2ee.core.pub.asynclog.LogDeal;
import com.sunmnet.j2ee.entity.core.ActionLog;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.sql.DataSource;
import java.sql.Connection;

/**
 * <PRE>
 * 数据库模块日志处理
 * 记录数据库DDL操作日志，拼接操作sql
 * </PRE>
 * <B>项    目：</B>J2EE开发平台
 * <B>技术支持：</B>三盟科技 (c) 2016
 *
 * @author 黄坚：huangjian@sunmnet.com
 * @version 1.0 2016/8/16
 * @since jdk版本：jdk1.7
 */
public class ModuleDeal extends LogDeal {

	/**
	 * log4j2日志
	 **/
	private static final Logger LOG = LogManager.getLogger(ModuleDeal.class.getName());

	public ModuleDeal() {
		super();
	}

	public ModuleDeal(DataSource datasource) {
		super(datasource);
	}

	@Override
	public void deal(ActionLog log) {
//		LOG.trace("deal");
//		Connection conn = getConn();
//		LOG.info("conn: {}, log: {}", conn, log);
//		close(conn);

		String method = log.getMethodName();
		//查询操作不记录
		if (isSelectMethod(method)) return;
		String className = log.getClassName();
		short type;

		if (className.contains("ModuleAction")) {
			LOG.info("模块操作: {}， 参数: {}", log.getMethodCnName(), log.getArgsContent());
		} else if (className.contains("DatabaseAction")) {
			if (method == "add") {
				type = 7;
			}
			LOG.info("数据库操作: {}， 参数: {}", log.getMethodCnName(), log.getArgsContent());
		} else if (className.contains("EntityAction")) {
			LOG.info("实体操作: {}， 参数: {}", log.getMethodCnName(), log.getArgsContent());
		} else if (className.contains("FieldAction")) {
			LOG.info("字段操作: {}， 参数: {}", log.getMethodCnName(), log.getArgsContent());
		} else if (className.contains("EntityRelateAction")) {
			LOG.info("关系操作: {}， 参数: {}", log.getMethodCnName(), log.getArgsContent());
		} else if (className.contains("IndexInfoAction")) {
			LOG.info("主键操作: {}， 参数: {}", log.getMethodCnName(), log.getArgsContent());
		} else if (className.contains("PDMAction")) {
			LOG.info("PDM操作: {}， 参数: {}", log.getMethodCnName(), log.getArgsContent());
		} else {
			LOG.info("建模设计操作: {}， 参数: {}", log.getMethodCnName(), log.getArgsContent());
		}
	}

	/**
	 * 判断是否查询方法
	 *
	 * @param methodName
	 * @return
	 */
	private boolean isSelectMethod(String methodName) {
		if (methodName.contains("get") || methodName.contains("find")) {
			return true;
		} else {
			return false;
		}
	}

}
